/***************************************************************************
 *   Copyright (C) 2008 by Rysiek Krol,,,   *
 *   rysiek@Babylon2   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "searchresultdialog.h"
#include "viewerevent.h"
#include "searchupdateevent.h"
#include "picturelistwidgetitem.h"
#include "picturemanager.h"
#include "eventhandler.h"
#include "searchdirupdateevent.h"

#include <QPixmap>
#include <QIcon>
#include <QMainWindow>

/**
 * Constructor
 * @param parent - Parent widget
 * @param fl - Window flags
 */
SearchResultDialog::SearchResultDialog(QString searchDir, QWidget* parent, Qt::WFlags fl)
        : QDialog( parent, fl ), Ui::SeachrResultDialog()
{
    setupUi(this);
    this->searchingInLine->setText(searchDir);
    this->isCanceled = false;
    this->setCursor(Qt::BusyCursor);
}

/**
 * Destructor
 */
SearchResultDialog::~SearchResultDialog()
{
}

/*$SPECIALIZATION$*/
/**
 * Starts new search
 */
void SearchResultDialog::newSearchSlot()
{
    QApplication::postEvent ( dynamic_cast<PictureManager*>(this->parent())->getEventHandler(), new ViewerEvent ( ViewerEvent::NewSearchEvent ) );
    QDialog::accept();
}

/**
 * Cancels search and exit window
 */
void SearchResultDialog::cancelSlot()
{
    this->isCanceled = true;
    this->searchingInLine->setText(tr("Search canceled"));
}

/**
 * Moves result to main window end exit
 */
void SearchResultDialog::moveResultToView()
{
    QListWidget* mainList=dynamic_cast<PictureManager*>(this->parent())->getListWidget();
    mainList->clear();
    QListWidgetItem* item = this->imageView->takeItem(0);
    while (item!='\0')
    {
        mainList->addItem(item);
        item = this->imageView->takeItem(0);
    }
    dynamic_cast<PictureManager*>(this->parent())->setIsAlbumViewer(true);
    this->accept();
}

/**
 * Exits window
 */
void SearchResultDialog::reject()
{
    QDialog::reject();
}

/**
 * Accept slot
 */
void SearchResultDialog::accept()
{
    QDialog::accept();
}

/**
 * Handels incomming events
 * @param event - Incomming event
 */
void SearchResultDialog::customEvent(QEvent * event)
{
    switch (event->type())
    {
    case ViewerEvent::SearchUpdateEvent:
    {
        if (!this->isCanceled)
        {
            SearchUpdateEvent *searchUpdateEvent = dynamic_cast<SearchUpdateEvent*>(event);

            for (QList<PictureListWidgetItem*>::iterator i = searchUpdateEvent->getFileList()->begin(); i != searchUpdateEvent->getFileList()->end(); i++)
            {
                (*i)->setIcon(QIcon(QPixmap::fromImage(*(*i)->getIconImage())));
                this->imageView->addItem((QListWidgetItem*)*i);
            }
            this->imageView->update();
        }
    }
    break;
            
    case ViewerEvent::SearchDirUpdateEvent:
    {
    		SearchDirUpdateEvent *searchUpdateDirEvent = dynamic_cast<SearchDirUpdateEvent*>(event);
            this->searchingInLine->setText(*searchUpdateDirEvent->getDir());
		if(this->searchingInLine->text()==tr("Search finished"))this->setCursor(Qt::ArrowCursor);
    
    }
    break;

    default:
        QMainWindow ( event );
        break;

    }

}


